using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace COME342.CollisionResolution.BinaryTree
{
    public partial class frmBSTTree : Form
    {
        private BinarySearchTree<string> sTreeOrig;
        private BinarySearchTree<string> shownTree;

        string key1 = "";
        bool cl = false;

        public frmBSTTree(BinarySearchTree<string> bst)
        {
            InitializeComponent();
            sTreeOrig = bst;
            UpdateTree(sTreeOrig);
        }

        void HighlightKeys(bool light)
        {            
            treeExControl1.Highlight(key1, light); 
        }

       public void UpdateTree(BinarySearchTree<string> bst)
        {
            shownTree = bst;
            TreeEx.NodeEx root;
            InitTree(out root, shownTree);
            treeExControl1.Root = root;
            treeExControl1.HighlightAll(false);
            //treeExControl1.Invalidate();
        }

        private void InitTree(out TreeEx.NodeEx root, BinarySearchTree<string> bst)
        {
            TreeEx.NodeEx ne = new TreeEx.NodeEx(bst.data.ToString());            
            if (!bst.IsLeftEmpty())
                FillTree(ref ne, bst.left);
            if (!bst.IsRightEmpty())
                FillTree(ref ne, bst.right);
            root = ne;
        }

        private void FillTree(ref TreeEx.NodeEx ret, BinarySearchTree<string> bst)
        {
            TreeEx.NodeEx ne = new TreeEx.NodeEx(bst.data.ToString());            
            ret.Add(ne);
            if (!bst.IsLeftEmpty())
                FillTree(ref ne, bst.left);
            if (!bst.IsRightEmpty())
                FillTree(ref ne, bst.right);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            key1 = txtItem.Text;
            sTreeOrig.Insert(key1);
            UpdateTree(sTreeOrig);            
            HighlightKeys(true);
            txtItem.Focus();
            txtItem.SelectAll();
        }

        private void button2_Click(object sender, EventArgs e)
        {            
            sTreeOrig.left = null;
            sTreeOrig.right = null;
            sTreeOrig.data = txtItem.Text;
            UpdateTree(sTreeOrig);
        }

        private void btnAddRange_Click(object sender, EventArgs e)
        {            
            int i = (int)numericUpDown1.Value;

            Random x = new Random();
            while (i-- != 0)
            {
                sTreeOrig.Insert(util.RandomString(7, x.Next(), true));                
            }

            UpdateTree(sTreeOrig);
            HighlightKeys(true);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            cl = true;
            this.Close();
        }

        private void frmBSTTree_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = !cl;
        }

    }
}